package com.drink.yan.svc.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.drink.yan.core.mapper.YanRequirementMapper;
import com.drink.yan.core.mapper.YanTaskDealMapper;
import com.drink.yan.core.mapper.YanTaskMapper;
import com.drink.yan.intf.define.*;
import com.drink.yan.intf.entity.YanCommission;
import com.drink.yan.intf.entity.YanRequirement;
import com.drink.yan.intf.entity.YanTask;
import com.drink.yan.intf.entity.YanTaskDeal;
import com.drink.yan.intf.vo.CustomerTaskSummaryVo;
import com.drink.yan.intf.vo.RequirementVo;
import com.drink.yan.intf.vo.TaskVo;
import com.drink.yan.intf.vo.YanTaskDealVo;
import com.drink.yan.web.rest.req.cmd.ApproveTaskCmd;
import com.drink.yan.web.rest.req.cmd.CancelTaskCmd;
import com.drink.yan.web.rest.req.cmd.DealRequireCmd;
import com.drink.yan.web.rest.req.cmd.SaveCustomerTaskCmd;
import com.drink.yan.web.rest.req.qry.PageRequireQry;
import com.drink.yan.web.rest.req.qry.PageTaskQry;
import com.meta.act.app.service.ISysDictTypeService;
import com.meta.act.app.service.ISysUserService;
import com.meta.framework.act.entity.SysUser;
import com.meta.framework.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * java类简单作用描述
 *
 * @Title:
 * @Package: com.drink.yan.svc.service.
 * @Author: M.simple
 * @Remark: The modified content
 * @CreateDate: 2023/11/24 20:44
 * @Version: v2.0
 */
@Service
public class YanRequirementService extends ServiceImpl<YanRequirementMapper, YanRequirement> {

	@Resource
	private ISysUserService iSysUserService;

	@Resource
	private ISysDictTypeService iSysDictTypeService;

	public List<RequirementVo> listRequire(PageRequireQry qry) {

		List<RequirementVo> taskVos = this.baseMapper.listRequire(qry);
		Map<String, String> map = iSysDictTypeService.selectDictMapByType("yan_require_status");

		taskVos.forEach(x -> {
			x.setRequireImages(JSONObject.parseArray(x.getRequireImagesStr(), String.class));
			x.setRequireStatusName(map.get(x.getRequireStatus()));
		});
		return taskVos;
	}

	public YanRequirement saveRequire(YanRequirement cmd) {

		cmd = saveRequire(cmd, true);

		return cmd;
	}

	public YanRequirement saveRequire(YanRequirement cmd, boolean loginFlag) {
		if (Objects.isNull(cmd.getRequireNo())) {
			cmd.setCreateBy(SecurityUtils.getUserId());
			cmd.setRequireStatus(YanRequireStatusE.commit_ed.getCode());
			this.baseMapper.insert(cmd);
		} else {
			YanRequirement task = getYanRequire(cmd.getRequireNo());
			Optional.ofNullable(cmd.getRequireContent()).ifPresent(task::setRequireContent);
			Optional.ofNullable(cmd.getRequireImages()).ifPresent(task::setRequireImages);
			Optional.ofNullable(cmd.getRequireType()).ifPresent(task::setRequireType);
			Optional.ofNullable(cmd.getRequireTypeName()).ifPresent(task::setRequireTypeName);
			Optional.ofNullable(cmd.getRequireStatus()).ifPresent(task::setRequireStatus);

			if(loginFlag){
				task.setUpdateBy(SecurityUtils.getUserId());
			}


			this.baseMapper.updateById(task);
			cmd = task;
		}
		return cmd;
	}

	private YanRequirement getYanRequire(Long requireNo) {
		return this.baseMapper.selectById(requireNo);
	}


	public RequirementVo getDetail(Long taskNo) {

		YanRequirement yanTask = getYanRequire(taskNo);
		if (Objects.isNull(yanTask)) {
			return null;
		}

		RequirementVo transform = yanTask.transform(RequirementVo.class);
		if(Objects.nonNull(transform.getCreateBy())){
			SysUser sysUser = iSysUserService.selectUserById(transform.getCreateBy());
			transform.setCreateByName(Optional.ofNullable(sysUser).orElse(new SysUser()).getNickName());
		}
		return transform;
	}

	public YanRequirement dealSucc(DealRequireCmd cmd) {
		YanRequirement yanTask = getYanRequire(cmd.getRequireNo());
		yanTask.setRequireStatus(YanRequireStatusE.deal_ed.getCode());
		saveRequire(yanTask);

		return yanTask;
	}

	public YanRequirement completeSucc(DealRequireCmd cmd) {
		YanRequirement yanTask = getYanRequire(cmd.getRequireNo());
		yanTask.setRequireStatus(YanRequireStatusE.complete_ed.getCode());
		yanTask.setRemark(cmd.getRemark());
		saveRequire(yanTask);

		return yanTask;
	}
}
